﻿using Newtonsoft.Json;
using Serilog.Core;
using SupDataSync.Model;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace DataSyncCommon
{
    public class HttpHelper
    {
        static Logger logger;
        public HttpHelper()
        {
            logger = LogHelper.CreateLoger();
        }
        public static Task<string> GetIpconfig()
        {
            var httpClient = new HttpClient();
            var str = httpClient.GetStringAsync("http://pv.sohu.com/cityjson");
            return str;
        }
        public static string RequestGet(string Url)
        {
            string PageStr = string.Empty;//用于存放还回的html
            Uri url = new Uri(Url);//Uri类 提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。就是处理url地址
            try
            {
                HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(url);//根据url地址创建HTTpWebRequest对象 
                httprequest.Method = "get";
                HttpWebResponse response = (HttpWebResponse)httprequest.GetResponse();//使用HttpWebResponse获取请求的还回值
                Stream steam = response.GetResponseStream();//从还回对象中获取数据流
                StreamReader reader = new StreamReader(steam, Encoding.UTF8);//读取数据Encoding.GetEncoding("gb2312")指编码是gb2312，不让中文会乱码的
                PageStr = reader.ReadToEnd();
                reader.Close();
                ApiRootModel model = JsonConvert.DeserializeObject<ApiRootModel>(PageStr);
                var str = JsonConvert.SerializeObject(model.Data);
                var str1 = model.Data.ToString();
                return str1;
            }
            catch (Exception err)
            {
                logger.Error("执行WebRequestApi异常：" + err.Message);
                throw err;
            }
        }
        /// <summary>
        /// 调用api返回json
        /// </summary>
        /// <param name="url"></param>
        /// <param name="jsonstr"></param>
        /// <param name="type">post</param>
        /// <returns></returns>
        public static string WebRequestApi(string url, string jsonstr, string type)
        {
            string result = "";
            try
            {
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
                request.Accept = "text/html,application/xhtml+xml,*/*";
                request.ContentType = "application/json";
                request.Method = type.ToUpper().ToString();//get或者post  
                request.Timeout = SysConfig.TimeOutSecond * 1000;
                byte[] buffer = encoding.GetBytes(jsonstr);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }

                ApiRootModel model = JsonConvert.DeserializeObject<ApiRootModel>(result);
                var str = JsonConvert.SerializeObject(model.Data);
                var str1 = model.Data.ToString();
                return str1;
            }
            catch (Exception err)
            {
                logger.Error("执行WebRequestApi异常：" + err.Message);
                throw err;
            }
        }
    }
}
